package com.zxgllhh.generator.tydic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.zxgllhh.config.FreemarkerManager;
import com.zxgllhh.domain.Table;
import com.zxgllhh.utils.PropertyUtils;

import freemarker.template.Template;
/**
 * 
 * @author Zhao,XingGuo
 *
 */
public class DaoGenarator {

	public static void go(Table table) throws Exception {
		// init
		String projectName = PropertyUtils.getProperty("projectName");
		String companyName = PropertyUtils.getProperty("companyName");
		String className = table.getClassName();
		String generatTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
		//prepare
		Map<String, Object> map = new HashMap<String, Object>();
		Template template = FreemarkerManager.getDaoTemplate();
		map.put("projectName", projectName);
		map.put("companyName", companyName);
		map.put("className", className);
		map.put("generatTime", generatTime);
		//execute
		FileUtils.forceMkdir(new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao"));
		File file = new File("E://generatorCode/src/com/"+companyName+"/" + projectName + "/dao/" + className + "Dao.java");
		Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
		template.process(map, writer);
	}

}
